<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch">
      <el-form-item label="规格">
        <el-input v-model="queryParams.specName" placeholder="请输入规格" maxlength="30" clearable style="width: 200px; margin-right: 10px;"/>  至
        <el-input v-model="queryParams.edSpecName" placeholder="请输入规格" maxlength="30" clearable style="width: 200px; margin: 0 10px;"/>
      </el-form-item>
      <el-form-item>
        <el-button style="margin-left: 20px" type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" ref="refbut">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus"  @click="handleAdd" v-hasPermi="['archive:warehouseManagement:filingBox:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete"  @click="handleDelete" v-hasPermi="['archive:warehouseManagement:filingBox:delete']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filingBoxList" :height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="specId"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button  type="text" icon="el-icon-delete" @click="handleDeleteRow(scope.row)" v-hasPermi="['archive:warehouseManagement:filingBox:delete']">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="规格数" prop="specName" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="单位" prop="specDw" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="页数" prop="spectPage" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="盒子数目" prop="specCount" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="剩余盒子数" prop="specAcount" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="已用盒子数" prop="specUsed" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="备注" prop="bz" align="center" :show-overflow-tooltip="true" />
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加档案盒 -->
    <el-dialog :title="title" :visible.sync="open" width="35%" append-to-body>
      <el-form ref="boxForm" :model="boxForm" :rules="boxRules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="规格" prop="specName">
              <el-input-number v-model="boxForm.specName" controls-position="right" :min="0"  placeholder="请输入规格" style="width: 100%;"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="单位" prop="specDw">
              <el-select v-model="boxForm.specDw" placeholder="请选择单位" style="width: 100%;">
                <el-option v-for="item in specDwOption" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="可装页数" prop="spectPage">
              <el-input-number v-model="boxForm.spectPage"  :precision="0" controls-position="right" placeholder="请输入可装页数" style="width: 100%;" :min="0" onKeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))" ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="bz">
              <el-input v-model="boxForm.bz" type="textarea" placeholder="请输入内容" ref="textareaRef" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button  @click="boxCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import estimateTableHeight from '@/mixins/estimateTableHeight'
import {addBox, deleteBox, getBoxList} from "@/api/archive/warehouseManagement/filingBox";
export default {
  name: "index",
  mixins: [estimateTableHeight], // 混入方法
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum:1,
        pageSize:10,
        specName: undefined,
        edSpecName: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案盒表格数据
      filingBoxList: [],
      // 选中数组
      ids: [],
      specNames:[],
      //全部的盒子数目
      specCounts:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层表单
      boxForm: {},
      // 弹出层表单校验
      boxRules: {
        specName: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ],
        specDw: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        spectPage: [
          { required: true, message: "可装页数不能为空", trigger: "blur" }
        ]
      },
      // 单位下拉框
      specDwOption: [
        {value: '0', label: 'mm'},
        {value: '1', label: 'cm'}
      ]
    }
  },
  created() {
    this.getList();
  },
  methods: {
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      var reg = /^(0|[1-9]\d*)(\.\d+)?$/
      if(this.queryParams.specName != undefined && this.queryParams.specName != '' && this.queryParams.specName != null) {
        if(reg.test(this.queryParams.specName) == false) {
          this.$message({ message: '规格数请输入数字', type: 'warning'})
          this.queryParams.specName = undefined
          return
        }
      }
      if(this.queryParams.edSpecName != undefined && this.queryParams.edSpecName != '' && this.queryParams.edSpecName != null) {
        if(reg.test(this.queryParams.edSpecName) == false) {
          this.$message({ message: '规格数请输入数字', type: 'warning'})
          this.queryParams.edSpecName = undefined
          return
        }
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.specName = undefined
      this.queryParams.edSpecName = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取表格数据
    getList() {
      this.loading = true
      getBoxList(this.queryParams).then(response => {
        this.total = response.total
        this.filingBoxList = response.rows
        for (var i = 0; i < response.rows.length; i++) {
          // 渲染表格单位
          for (let j = 0; j < this.specDwOption.length; j++) {
            if(response.rows[i].specDw == this.specDwOption[j].value){
              this.filingBoxList[i].specDw = this.specDwOption[j].label
            }
          }
          if(response.rows[i].specCount == null) {
            this.filingBoxList[i].specCount = 0
          }
          if(response.rows[i].specAcount == null) {
            this.filingBoxList[i].specAcount = 0
          }
          this.filingBoxList[i].specUsed = response.rows[i].specCount - response.rows[i].specAcount
        }
        this.loading = false
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.specId)
      this.specNames=selection.map(item => item.specName)
      this.specCounts=selection.map(item => item.specCount)
      this.single = selection.length!==1
      this.multiple = !selection.length

    },
    // 重置表单
    reset() {
      this.boxForm = {
        specName: undefined,
        specDw: undefined,
        spectPage: undefined,
        specCount: undefined,
        bz: undefined
      }
      this.$nextTick(() => {
        this.$refs.textareaRef.$refs.textarea.style.height = 'auto'; // 重置高度
      });
    },
    // 新增
    handleAdd() {
      this.reset();
      this.resetForm('boxForm')
      this.title = "新增档案盒"
      this.open = true
    },
    // 删除按钮
    handleDelete(row) {
      const specIds = row.id || this.ids;
      const specNames=row.specName||this.specNames;
      const specCounts=row.specCount||this.specCounts;
      if(specIds.length<=0){
        this.$modal.msgWarning('请选择数据！')
        return false;
      }
      var num=0;
      for(var n in specCounts){
        num+=specCounts[n];
      }
      if(num>0){
        this.$modal.msgWarning('所选规格数据中存在档案盒数据 不允许删除！')
        return false;
      }
      this.$modal.confirm('是否确认删除规格数为"' + specNames + '"的数据项？').then(function() {
        return deleteBox(specIds)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 行内删除
    handleDeleteRow(row) {
      const specId = row.specId
      const specName=row.specName;
      if(row.specCount>0){
        this.$modal.msgWarning('档案盒规格中存在数据不能删除！')
        return false;
      }
      this.$modal.confirm('是否确认删除规格数为"' + specName + '"的数据项？').then(function() {
        return deleteBox(specId)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 弹窗取消
    boxCancel() {
      this.open = false;
    },
    submitForm: function () {
      this.$refs["boxForm"].validate(valid => {
        if (valid) {
          if(this.boxForm.specId != undefined){
          } else {
            var reg = /^[0-9]{1,9}$/
            if(reg.test(this.boxForm.spectPage) == false) {
              this.$message({ message: '可装页数位数请小于10位', type: 'warning'})
              return
            }
            addBox(this.boxForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}

::v-deep .el-dialog .el-dialog__body {
  flex: 1;
  overflow: auto;
}
</style>
